var ctx;
var phoneReg = /^(0[0-9]{2,3}\-?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0-9]|18[5|6|8|9])\d{8}$)/;
var emailReg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;

function setCtx(ctx){
	this.ctx = ctx;
}

//验证手机
function checkPhone(){
	 var phone = $("#phone").val();
	//解决ie9以下placeholder与value重复的问题
		if(phone == $("#phone").attr("placeholder")){
			phone = "";
		}

			if(phone == null || phone == ""){
				layer.tips('手机号不能为空', '#phone', {
				    tips: 3,
				    time:1000
				});
				return false;
			}else{
				if(!phoneReg.test(phone)){
					layer.tips('手机号格式错误，请填写正确格式的手机号!有区号的必须加上短横线，如：027-87878787；027-59595959', '#phone', {
					    tips: 3,
					    time:1000
					});
					return false;
				}else{
					return true;
				}
			}
		
}

//验证邮箱
function checkEmial(){
	 var email = $("#email").val();
	//解决ie9以下placeholder与value重复的问题
		if(email == $("#email").attr("placeholder")){
			email = "";
		}

			if(email == null || email == ""){
				layer.tips('邮箱不能为空', '#email', {
				    tips: 3,
				    time:1000
				});
				return false;
			}else{
				if(!emailReg.test(email)){
					layer.tips('邮箱格式错误，请填写正确格式的邮箱!', '#email', {
					    tips: 3,
					    time:1000
					});
					return false;
				}else{
					return true;
				}
			}	
}

//保存方法
function savePersonalInfo() {
	var loginname = $('#loginname').val();
	var phone = $('#phone').val();
	var nickname = $('#nickname').val();
	var username = $('#username').val();
	var email = $('#email').val();
	var sex = $("input[name='sex']:checked").val();
	var imageLogo = $('#imageLogo').val();
	var lastLoginTime = $('#lastLoginTime').val();
	var loginTimes = $('#loginTimes').val();
	var userId = $('#userId').val();
	
	if( checkPhone() && checkEmial()){
		$.ajax({
			type : "POST",
			url : ctx + "/personalInfo/UpdatePersonalInfo",
			cache : false,
			dataType : 'json',
			data: {
				loginname : loginname,
				 phone : phone,
				 nickname : nickname,
				 username : username,
				 email : email,
				 imageLogo:imageLogo,
				 sex:sex,
				 lastLoginTime : lastLoginTime,
				 loginTimes : loginTimes
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功",{
						icon: 1,
						time: 3000 //2秒关闭（如果不配置，默认是3秒）
					},function(){
						loadContent("/personalInfo/personalInfoList");
					});
				} else {
					layer.msg("操作失败",{
						icon: 2
					});
				}
			}
		});
	}		
}

//返回上一页
function cancel(){
	parent.loadContent("/personalInfo/personalInfoList");
}